
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ListGUI implements ActionListener {
	// Actual CSV File Being Read in
	protected CSVReader csv;
	// GUIs
	private JPanel      csvPanel;
	// HashTable references the CheckBox to the PlayerData
	private Hashtable <Integer, ArrayList<Statistic>> cb2PlayerData;
	
	
	// ArrayList contains all of the actual CheckBoxes to get the unique hash.
	private ArrayList<JCheckBox>  cbArrayList;
	private Button                submitToTags;
	private JTextArea             outputTextArea;
	private Button                stopButton; // lol
	private BattingOrderOptimizer boo;
	public ListGUI(CSVReader csvFile) throws IOException, WTFException {

		JFrame listFrame;
		JPanel mainPanel;
		JPanel secondPanel;
		JPanel submitToTagsPanel;
		JPanel outputPanel;

		listFrame          = new JFrame("CSV List - Select Players");
		mainPanel          = new JPanel();
		secondPanel        = new JPanel();
		submitToTagsPanel  = new JPanel();
		outputPanel		   = new JPanel();
		outputTextArea	   = new JTextArea(); 
		this.csvPanel      = new JPanel();
		this.submitToTags  = new Button("Submit to Simulator");
		this.stopButton    = new Button("Stop");
		this.cbArrayList   = new ArrayList<JCheckBox>();
		
		//set CSV to the new File and read it
		csv = csvFile;
		this.readCSV();

		//Main Frame
		listFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		listFrame.setSize(1000,750);
		listFrame.setVisible(true);		

		//Sets up the Grid Layout in the mainPanel AND the secondPanel
		mainPanel.setSize(250,250);
		mainPanel.setLayout(new GridLayout(0,1));	
		secondPanel.setSize(250, 100);
		secondPanel.setLayout(new GridLayout(2,1));
		secondPanel.add(submitToTagsPanel);
		secondPanel.add(new JScrollPane(outputPanel));
		mainPanel.add(new JScrollPane(this.csvPanel));
		mainPanel.add(secondPanel);
		//mainPanel.add(submitToTagsPanel);
		//mainPanel.add(new JScrollPane(outputPanel));	

		listFrame.add(mainPanel);

		//Shows how many more players have been selected.
		csvPanel.setLayout(new GridLayout(0, csv.getWidth() + 1, 0, 0 ));
		csvPanel.setVisible(true);

		//trying to setup output textarea
		outputPanel.add(outputTextArea);
		outputTextArea.setVisible(true);
		outputTextArea.setSize(250,250);
		outputPanel.setVisible(true);
		outputTextArea.setBounds(outputPanel.getX(), outputPanel.getY(), 100, 50);		

		outputTextArea.setEditable(false);		

		submitToTagsPanel.add(this.submitToTags);
		submitToTagsPanel.add(this.stopButton);
		submitToTagsPanel.setVisible(true);
		submitToTags.addActionListener(this);
		submitToTags.setEnabled(false);

		stopButton.addActionListener(this);
		
	}// Ends List GUI Constructor

	public void addTextular( String tex ) {
		this.outputTextArea.append(tex);
	}

	public void textClear() {
		this.outputTextArea.setText("");
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == submitToTags){
			//disable submit button.
			submitToTags.setEnabled(false);

			int               maxPlayers   = 9;
			int               cap          = 0;
			ArrayList<Player> selectedList = new ArrayList<Player>();

			for ( 
					int c = 0; // count
					c < cbArrayList.size() && // through the whole array
					cap <= maxPlayers;    // but not more then 9 ( maxPlayers )
					++c // kick'er up
			){
				JCheckBox tempBox = cbArrayList.get(c);  
				if ( tempBox.isSelected() ) {
					Player player = new Player(cb2PlayerData.get(tempBox.hashCode()));
					player.setID(c);
					selectedList.add(player);
				}// end if selected
			}// ends for each element in the cbArrayList
			cap++;
			cerranoSim( selectedList );
		} // end tags Kick-off

		if ( e.getSource().getClass().getName().equals("javax.swing.JCheckBox") ) { // DON'T FSCK THIS UP.
			int checked = 0;
			for ( int i = 0; checked < this.cbArrayList.size() && i < this.cbArrayList.size(); ++i )
				if ( this.cbArrayList.get(i).isSelected() )
					checked++;

			if ( checked >= 9 ) {
				// grey out all boxen
				for ( int i = 0; i < this.cbArrayList.size(); ++i ) {
					if ( ! this.cbArrayList.get(i).isSelected() ) // only check off unselected
						this.cbArrayList.get(i).setEnabled(false);
					submitToTags.setEnabled(true);
				}
			} else { // if it's less then 9 checked
				submitToTags.setEnabled(false);
				for ( int i = 0; i < this.cbArrayList.size(); ++i )
					this.cbArrayList.get(i).setEnabled(true);
			} // end check for 9 boxes
		} // ends if JCheckBox
		
		if(e.getSource() == stopButton){
			if ( this.boo == null ) {
				System.out.println("no means yes");
				return;
			}
			
			System.out.println("stopping");
			this.submitToTags.setEnabled(true);
			this.boo.decap_chicken();
		}
		
		
	} // Ends actionPreformed()

	public void readCSV() throws WTFException{
		cb2PlayerData = new Hashtable<Integer, ArrayList<Statistic>>();
		if ( csv != null ) {
			csv.reset();
			try {
				while (csv.hasNext() ) {
					ArrayList<Statistic> s1  = csv.getNextStatLine();
					JCheckBox playerCheck = new JCheckBox();
					playerCheck.addActionListener(this);
					cb2PlayerData.put(playerCheck.hashCode(),s1); // text at the (butt)hash ID
					cbArrayList.add(playerCheck);
					csvPanel.add(playerCheck);
					for ( int i = 0; i < s1.size(); ++i )
						csvPanel.add(new JLabel(s1.get(i).value));
				}//ends while
			} catch (IOException e) {				
				e.printStackTrace();
			}//ends catch
		}// ends null check
	} // ends readCSV()

	//tests simulator for selectedList only
	public void cerranoSim( ArrayList<Player> selectedList ){
		boo = new BattingOrderOptimizer(selectedList);
		boo.linkListGUI(this);
		boo.start();
	}// ends cerranoSim()
}// Ends Class

